package com.itheima.question.service.impl;

import com.itheima.common.contants.RedisKeyConstants;
import com.itheima.question.dto.QuestionDto;
import com.itheima.question.mapper.QuestionMapper;
import com.itheima.question.pojo.Question;
import com.itheima.question.service.IQuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Override
    public boolean addQuestion(QuestionDto questionDto) {
        Question question = new Question();
        BeanUtils.copyProperties(questionDto,question);
        int insert = questionMapper.insert(question);

        //将问题id存入redis的set集合中，用于动态审核
        String id = question.getId();
        redisTemplate.opsForSet().add(RedisKeyConstants.AUDIT_QUESTION,id);

        //保存图片id到redis中，清理垃圾图片使用的
        String pid = question.getPicture();
        redisTemplate.opsForSet().add(RedisKeyConstants.USE_PIC,pid.replace(fileServerUrl,""));
        return insert>0;
    }
}
